<!DOCTYPE html>
          <head>
        <meta charset="utf-8">
            
            <title>
                「Android」用support annotation替代enum | ttdevs
            </title>
            <meta content="width=device-width, initial-scale=1" name="viewport">
            <meta name="theme-color" content="#4184f3">
            
            
            <link href="/favicon.ico" rel="icon"/>
            

            <link rel="stylesheet" href="/css/highlight.light.css">
            <link rel="stylesheet" href="/css/prism-customize.css">
            <link rel="stylesheet" href="/css/nav-icon.css">
            <link rel="stylesheet" href="/css/waves.min.css">
            <link rel="stylesheet" href="/css/jquery.tocify.css">
            <link rel="stylesheet" href="/css/main.css">
            <link rel="stylesheet" href="/css/nav-indicator.css">
            
  

  
  <!-- 谷歌统计 -->
  <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-97465173-1', 'auto');
    ga('send', 'pageview');

  </script>
  
            </meta>
        </meta>
    </head>

    <body>
        <header>
            <!-- cover image or sth. -->
        </header>
        <div id="main" class="m-scene">
            
<div class="nav-wrapper">

    <div class="container">
        <nav>
            <div class="logo wave">
                <a href="/" id="logo">
                    ttdevs
                </a>
            </div>
            <div class="nav-toggle-icon" >
                <div class="material-hamburger">
                    <span>
                    </span>
                    <span>
                    </span>
                    <span>
                    </span>
                </div>
            </div>
            <div class="menu-wrapper">
                <div class="nav-indicator">
                </div>
                <ul class="menus">
                    
                     
                        <li>
                            <a class="wave " href="/">
                                首页
                            </a>
                        </li>
                     
                        <li>
                            <a class="wave " href="/archives">
                                归档
                            </a>
                        </li>
                     
                        <li>
                            <a class="wave " href="/about">
                                关于
                            </a>
                        </li>
                     
                    
                   
                </ul>
            </div>
        </nav>
    </div>
</div>
            <div class="container content">
                <div class="scene_element scene_element--fadein">
                    <div class="row">
    <div class="main">
        <article>
          
          <header class="post-header with-cover" style="background-image:url('/1970/01/01/[Android]用support annotation替代enum/cover.jpg')" >
          
          </header>
          <h1 class="post-title">「Android」用support annotation替代enum</h1>

          <section class="post-info">
            <span class="post-date">1970/01/01</span>
            
            <span class="post-category">
                <a class="article-category-link" href="/categories/技术/">技术</a>
            </span>
            
            
            <span class="post-tags">
              <ul class="post-tag-list"><li class="post-tag-list-item"><a class="post-tag-list-link" href="/tags/Android/">Android</a></li></ul>
            </span>
            
          </section>

          <section class="post-content">
            <blockquote>
<p>Allocation.MipmapControl,<br>Authenticator.RequestorType,<br>AvoidXfermode.Mode,<br>Bitmap.CompressFormat,<br>Bitmap.Config, BlurMaskFilter.Blur,<br>Canvas.EdgeType,<br>Canvas.VertexMode,<br>ClientInfoStatus,<br>ConsoleMessage.MessageLevel,<br>CursorJoiner.Result,<br>and 48 others.</p>
</blockquote>
<p>简单看了下，Android还是有好多地方使用了。那为什么还不建议使用呢？查了一番，看到google官方有一段关于enum使用的<a href="/1970/01/01/[Android]用support annotation替代enum/1">介绍</a>。既然使用不当可能会导致性能问题，那我们怎么办呢？</p>
<p>这里介绍下Support包中的annotation，使用 <code>@IntDef</code> 和 <code>@StringDef</code> 来达到我们枚举的效果。由于<code>@IntDef</code> 的介绍比较多，这里给个<code>@StringDef</code> 的Demo：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyClient</span> </span>&#123;</div><div class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> String ONE = <span class="string">"one"</span>;</div><div class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> String RECORD = <span class="string">"record"</span>;</div><div class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> String STATUS = <span class="string">"status"</span>;</div><div class="line"></div><div class="line">    <span class="meta">@StringDef</span>(&#123;ONE, RECORD, STATUS&#125;)</div><div class="line">    <span class="keyword">public</span> <span class="meta">@interface</span> Client &#123;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="keyword">private</span> String mClient;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="title">MyClient</span><span class="params">(@Client String client)</span> </span>&#123;</div><div class="line">        mClient = client;</div><div class="line">    &#125;</div><div class="line">    ...</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>这样，在外面创建MyClient对象的时候，构造方法中传入的参数就必须为 <code>ONE, RECORD, STATUS</code> 其中之一，从而达到类似enum的效果。</p>
<h2 id="参考"><a href="/1970/01/01/[Android]用support annotation替代enum/#参考" class="headerlink" title="参考"></a>参考</h2><ol>
<li><a href="https://noobcoderblog.wordpress.com/2015/04/12/java-enum-and-android-intdefstringdef-annotation/" target="_blank" rel="external">https://noobcoderblog.wordpress.com/2015/04/12/java-enum-and-android-intdefstringdef-annotation/</a></li>
<li><a href="https://www.youtube.com/watch?v=Hzs6OBcvNQE" target="_blank" rel="external">The price of ENUMs</a></li>
<li><a href="http://tools.android.com/tech-docs/support-annotations" target="_blank" rel="external">http://tools.android.com/tech-docs/support-annotations</a></li>
<li><a href="http://developer.android.com/reference/java/lang/Enum.html" target="_blank" rel="external">http://developer.android.com/reference/java/lang/Enum.html</a></li>
<li><a href="http://stackoverflow.com/questions/29183904/should-i-strictly-avoid-using-enums-on-android" target="_blank" rel="external">http://stackoverflow.com/questions/29183904/should-i-strictly-avoid-using-enums-on-android</a></li>
</ol>
<p><img src="https://raw.githubusercontent.com/ttdevs/ttdevs.github.io/common/images/logo.png" alt="Create by ttdevs"></p>

          </section>
        </article>
        

       
        <div class="pager">
          
            <a class="post-prev pager-item" href="/1970/01/01/[Android]内存泄漏相关内容总结(上)/" >
              <strong class="article-nav-caption">上一篇</strong>
              <p class="post-nav-title">「Android」内存泄漏相关内容总结(上)</p>
            </a>
          
          
            <a class="post-next pager-item" href="/1970/01/01/[Android]各种小记录/">
              <strong class="article-nav-caption">下一篇</strong>
              <p class="post-nav-title">「Android」各种小记录</p>
            </a>
          
        </div>
        

         <!-- comments -->
        <div class="comment-section">
  
    


</div>

    </div>
    
    <aside>
        <div id="toc">
        </div>
    </aside>
    
</div>

                </div>
            </div>
        </div>
        <footer class="footer">
    <p>由<a href="http://hexo.io/" target="_blank">Hexo</a>强力驱动，搭载<a href="https://github.com/wayou/hexo-theme-gstyle">gstyle</a>主题</p>
    <p>
        &copy; 2017 ttdevs
    </p>
</footer>
<script src="/lib/jquery.js"></script>
<script src="/lib/waves.js"></script>
<script src="/lib/jquery-ui.js"></script>
<script src="/lib/jquery.tocify.js"></script>
<script src="/js/main.js"></script>

    </body>
</html>
